const getAllScores = () => {
    return localStorage.getItem('scoreData')
        ? JSON.parse(localStorage.getItem('scoreData'))
        : []
}
const setAllScores = (data) => {
    localStorage.setItem('scoreData', JSON.stringify(data))
}

// 查询数据
export const selectScoreAsync = () => {
    return new Promise((resolve) => {
        setTimeout(() => {
            const scoreData = getAllScores()
            resolve({ code: 0, data: scoreData })
        }, 500)
    })
}

// 根据学号，对所有成绩单新增成绩项
export const addStudentScoreAsync = (studentNo) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            let scoreData = getAllScores()
            scoreData = scoreData.map((item) => {
                item.list.push({
                    studentNo,
                    chinese: null,
                    math: null,
                    eglish: null,
                    physics: null,
                    chemistry: null,
                    biology: null
                })
                return item
            })
            setAllScores(scoreData)
            resolve({ code: 0 })
        }, 500)
    })
}
// 根据学号，对所有成绩单删除成绩项
export const removeStudentScoreAsync = (studentNo) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            let scoreData = getAllScores()
            scoreData = scoreData.map((item) => {
                item.list = item.list.filter(
                    (cell) => +cell.studentNo !== +studentNo
                )
                return item
            })
            setAllScores(scoreData)
            resolve({ code: 0 })
        }, 500)
    })
}

// 根据考试编号，添加整张成绩单
export const addExamScoreAsync = (examNo, data) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            let scoreData = getAllScores()
            scoreData.push({ examNo, list: data, type: 'SCORE' })
            setAllScores(scoreData)
            resolve({ code: 0 })
        })
    })
}
// 根据考试编号，删除整张成绩单
export const removeExamScoreAsync = (examNo) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            let scoreData = getAllScores()
            scoreData = scoreData.filter((item) => +item.examNo !== +examNo)
            setAllScores(scoreData)
            resolve({ code: 0 })
        }, 500)
    })
}

// 修改数据
export const updateScoreAsync = (data, oldData) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            let scoreData = getAllScores()
            scoreData = scoreData.filter((item) => item.no !== oldData.no)
            scoreData.push(data)
            scoreData.sort((a, b) => {
                return +a.no - +b.no
            })
            setAllScores(scoreData)
            resolve({ code: 0 })
        }, 500)
    })
}

// 删除数据
export const deleteScoreAsync = (examNo, studentNo) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            let scoreData = getAllScores()
            scoreData = scoreData.map((item) => {
                if (+item.examNo === examNo) {
                    item.list.map((cell) => {
                        if (+cell.studentNo === +studentNo) {
                            cell.chinese = null
                            cell.math = null
                            cell.eglish = null
                            cell.physics = null
                            cell.chemistry = null
                            cell.biology = null
                        }
                        return cell
                    })
                }
                return item
            })
            setAllScores(scoreData)
            resolve({ code: 0 })
        }, 500)
    })
}
